<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/9/23
 * Time: 11:33
 */

namespace app\admin\controller;


class GoodsType extends Base
{
    /**
     * 商品模型
     */
    public function goodsModel(){
        $list = model('GoodsModel') -> paginate(10);
        $this -> assign('list', $list);

        return $this -> fetch();
    }

    public function goodsModel_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('GoodsModel') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('goodsType/goodsModel'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            if($id){
                $result = model('GoodsModel') -> find($id);
            }

            $this -> assign('result', $result);
            $this -> assign('id', $id);
            return $this -> fetch();
        }
    }

    public function goodsModel_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = $this -> validate($data, 'GoodsModel.del');
            if(true !== $result){
                return $this -> error($result, '', ['token' => $this -> request -> token()]);
            }

            $res = model('GoodsModel') -> del($data['id']);
            if($res['code']){
                return $this -> success($res['msg'], url('goodsType/goodsModel'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 商品规格
     */
    public function goodsSpec(){
        $search_key = input();
        unset($search_key['/admin/goods_type/goodsspec_html']);

        $arr = model('GoodsSpec') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('model', $arr['model']);
        $this -> assign('list', $arr['list']);

        return $this -> fetch();
    }

    public function goodsSpec_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('GoodsSpec') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('goodsType/goodsSpec'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            if($id){
                $result = model('GoodsSpec') -> resFind($id);
            }

            $model = model('GoodsModel') -> select();

            $this -> assign('result', $result);
            $this -> assign('model', $model);
            $this -> assign('id', $id);
            return $this -> fetch();
        }
    }

    public function goodsSpec_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('GoodsSpec') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('goodsType/goodsSpec'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 商品属性
     */
    public function goodsAttr(){
        $search_key = input();
        unset($search_key['/admin/goods_type/goodsattr_html']);

        $arr = model('GoodsAttr') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('model', $arr['model']);
        $this -> assign('list', $arr['list']);

        return $this -> fetch();
    }

    public function goodsAttr_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('GoodsAttr') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('goodsType/goodsAttr'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $result = null;
            if($id){
                $result = model('GoodsAttr') -> find($id);
            }
            $model = model('GoodsModel') -> select();

            $this -> assign('result', $result);
            $this -> assign('model', $model);
            $this -> assign('id', $id);
            return $this -> fetch();
        }
    }

    public function goodsAttr_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('GoodsAttr') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('goodsType/goodsAttr'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }
}